package com.jiangjiang.eshop.commodity.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.jiangjiang.eshop.commodity.dto.GoodsSkuDTO;
import com.jiangjiang.eshop.commodity.dto.GoodsSkuPageQueryDTO;
import com.jiangjiang.eshop.commodity.service.GoodsSkuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName GoodsSkuController
 * @Description 商品SKU管理
 * @Author jhw
 * @Date 2021/8/2 16:15
 * @Version 1.0
 **/
@Slf4j
@RestController
@RequestMapping("/commodity/goods/sku")
public class GoodsSkuController {
    @Autowired
    private GoodsSkuService goodsSkuService;


    /**
     * 根据商品id查询商品sku
     * @param goodsId 商品id
     * @return 商品sku
     * @throws Exception
     */
    @GetMapping("/queryByGoodsId")
    public List<GoodsSkuDTO> queryByGoodsId(@RequestParam("goodsId") Long goodsId) {
        try {
            return goodsSkuService.queryByGoodsId(goodsId);
        } catch (Exception e) {
            log.error("error", e);
            return Lists.newArrayList();
        }
    }

    /**
     * 批量新增商品sku
     * @param goodsSkuList 商品数据
     * @return
     */
    @PostMapping("/batchSave")
    public Boolean batchSave(@RequestBody List<GoodsSkuDTO> goodsSkuList) {
        try {
           return goodsSkuService.batchSave(goodsSkuList);
        } catch (Exception e) {
            log.error("error", e);
            return false;
        }
    }

    /**
     * 根据商品id删除sku
     * @param goodsId 商品id
     */
    @GetMapping("/removeByGoodsId")
    public Boolean removeByGoodsId(@RequestParam("goodsId") Long goodsId) {
        try {
            return goodsSkuService.removeByGoodsId(goodsId);
        } catch (Exception e) {
            log.error("error", e);
            return false;
        }
    }

    /**
     * 分页查询商品sku
     * @param dto
     * @return
     */
    @GetMapping("/queryForPage")
    public Page<GoodsSkuDTO> queryForPage(GoodsSkuPageQueryDTO dto) {
        try {
            return goodsSkuService.queryForPage(dto);
        } catch (Exception e) {
            log.error("error", e);
        }
        return null;
    }

}
